Zeichen's app for both server and client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import ORM, { DatabaseKind } from '../../../utilities/ORM'
  2. import * as Service from '../../../services/entities/Note'
  3. import Model from '../../../models/Note'
  4. export default async (req, res) => {
  5. const orm = new ORM({
  6. kind: process.env.DATABASE_DIALECT as DatabaseKind,
  7. url: process.env.DATABASE_URL,
  8. })
  9. const repository = orm.getRepository(Model)
  10. const methodHandlers = {
  11. 'GET': Service.getSingle(repository),
  12. 'PUT': Service.save(repository)(req.body),
  13. 'DELETE': Service.remove(repository)
  14. }
  15. const { [req.method as keyof typeof methodHandlers]: handler = null } = methodHandlers
  16. if (handler === null) {
  17. res.statusCode = 415
  18. res.json({ message: 'Method not allowed.' })
  19. return
  20. }
  21. const { id } = req.query
  22. try {
  23. const { status, ...etcReturn } = await handler(id)
  24. res.statusCode = status
  25. if (etcReturn['data']) {
  26. res.json(etcReturn['data'])
  27. return
  28. }
  29. res.end()
  30. } catch (err) {
  31. console.error(err)
  32. const { status, data, } = err
  33. res.statusCode = status || 500
  34. if (data && status !== 204) {
  35. res.json(data)
  36. return
  37. }
  38. res.end()
  39. }
  40. }